<template>
  <div class="dialog">
    <el-dialog :title="'品种' + titleText" :visible.sync="showDialog" width="35%" :close-on-click-modal="false"
      @close="cancel" :before-close='beforeClose'>
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="品种名称:" prop="varietyName">
          <el-input v-model="formData.varietyName" clearable placeholder="请输入品种名称	" />
        </el-form-item>
        <el-form-item label="养殖周期:">
          <div class="flex">
            <el-input style="width: 46%;" type="number" v-model="periodList[0]" @change="getperiodList" clearable
              placeholder="请输入养殖周期	">
              <template slot="append">天</template>
            </el-input>
            <div>至</div>
            <el-input style="width: 46%;" type="number" v-model="periodList[1]" @change="getperiodList" clearable
              placeholder="请输入养殖周期	">
              <template slot="append">天</template>
            </el-input>
          </div>
        </el-form-item>
        <!-- <el-form-item label="活鱼物料:"  prop="liveMaterialId">
          <el-select filterable clearable v-model="formData.liveMaterialId">
            <el-option v-for="(item, index) in materialList" :key="index" :label="item.name" :value="item.materialId" />
          </el-select>
        </el-form-item>
        <el-form-item label="次鱼物料:"  prop="secondaryMaterialId">
          <el-select filterable clearable v-model="formData.secondaryMaterialId">
            <el-option v-for="(item, index) in materialList" :key="index" :label="item.name" :value="item.materialId" />
          </el-select>
        </el-form-item>
        <el-form-item label="直鱼物料:"  prop="deadMaterialId">
          <el-select filterable clearable v-model="formData.deadMaterialId">
            <el-option v-for="(item, index) in materialList" :key="index" :label="item.name" :value="item.materialId" />
          </el-select>
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import FishVarietyApi from "@/api/basic-info-manage/fishVariety"
import MaterialApi from "@/api/basic-info-manage/material"

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    }
  },
  data() {
    let checkInt = (rule, value, callback) => {
      if (value) {
        if (Number(value) && value % 1 === 0) {
          callback();
        } else {
          return callback(new Error("请输入整数！"));
        }
      }
    };
    return {
      formData: {},
      materialList: [],
      rules: {
        varietyName: [
          { required: true, message: "请输入品种名称", trigger: ["change", "blur"] }
        ]
      },
      periodList: [],
      showDialog: this.show
    };
  },
  computed: {
  },
  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.periodList = []
        this.formData = {
          staffName: sessionStorage.getItem('staffName'),
        }
        this.$nextTick(() => {
          this.$refs['ruleForm'].clearValidate()
        })
      }
      if (v === 'edit') {
        this.formData = this.editObject
        this.editObject.liveMaterial ? this.formData.liveMaterialId = this.editObject.liveMaterial.materialId : ''
        this.editObject.secondaryMaterial ? this.formData.secondaryMaterialId = this.editObject.secondaryMaterial.materialId : ''
        this.editObject.deadMaterial ? this.formData.deadMaterialId = this.editObject.deadMaterial.materialId : ''
      }
      if (this.formData.period && this.formData.period.length) {
        this.periodList = this.formData.period.split('~');
      }
    }
  },
  created() {
  },
  mounted() {
    this.queryMaterial()
  },
  methods: {
    getperiodList() {
      this.periodList[0] ? this.periodList[0] : this.periodList[0] = 1
      this.periodList[1] ? this.periodList[1] : this.periodList[1] = 1
      if (this.periodList && this.periodList[0] && this.periodList[1]) {
        this.formData.period = this.periodList.join('~');
      } else {
        this.formData.period = this.periodList[0] || this.periodList[1]
      }
      if (this.periodList[0] && this.periodList[1] && this.periodList[1] - this.periodList[0] <= 0) {
        this.$message.info('请输入合理区间！')
        this.periodList = [this.periodList[0], Number(this.periodList[0]) + 1]
      }
    },
    queryMaterial() {
      MaterialApi.queryByCondition({ materialTypeNo: 'FISH' }).then(res => {
        this.materialList = res.data
      })
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            FishVarietyApi.edit(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                this.$message.error(res.message || '修改失败！');
              }
            }).catch(err => {
              this.$message.error('修改失败！');
            });
          } else if (this.nowChanceStau === "add") {
            FishVarietyApi.add(this.formData).then(res => {
              if (res.success === true) {
                this.$message.success("添加成功！");
                this.close()
              } else {
                this.$message.error(res.message);
              }
            }).catch(err => {
              this.$message.error('添加失败！');
            });
          }
        } else {
          return false;
        }
      });
    },
    beforeClose(done) {
      this.formData = {}
      done()
    },
    close() {
      this.$emit("closeDialog");
    },
    cancel() {
      this.$emit("cancelDialog");
      this.periodList = []
    }
  }
};
</script>

<style lang="scss" scoped>
.flex{
  width: 100%;
  display: flex;
  justify-content: space-between;
  flex-wrap: nowrap;
}
</style>
